package com.example.aircraftwar2024.activity;


import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.aircraftwar2024.R;
import com.example.aircraftwar2024.leaderBoard.ScoreBoardDAO;
import com.example.aircraftwar2024.leaderBoard.ScoreBoardImpl;
import com.example.aircraftwar2024.leaderBoard.ScoreRecord;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RankActivity extends AppCompatActivity implements View.OnClickListener{
    private ScoreBoardDAO scoreBoard;
    private int rank_clicked;
    private boolean music;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityManager.getActivityManager().addActivity(this);

        int gameMode=getIntent().getIntExtra("gameMode",0);
        int score=getIntent().getIntExtra("score",0);
        String name=getIntent().getStringExtra("username");
        long time=getIntent().getLongExtra("time",0);
        music = getIntent().getBooleanExtra("music", true);
        super.onCreate(savedInstanceState);
        DisplayMetrics dm = new DisplayMetrics();
        setContentView(R.layout.activity_record);

        TextView text = (TextView) findViewById(R.id.title);
        if (gameMode==0) text.setText("简单模式");
        else if (gameMode==1) text.setText("普通模式");
        else text.setText("困难模式");
        text.setX(365);

        scoreBoard=new ScoreBoardImpl();
        scoreBoard.setFile(gameMode);
        scoreBoard.readFromFile();
        scoreBoard.doAdd(score,name);

        //获得Layout里面的ListView
        ListView list = (ListView) findViewById(R.id.ListView01);
        List<Map<String, Object>> tmp=getData();
        //生成适配器的Item和动态数组对应的元素
        SimpleAdapter listItemAdapter = new SimpleAdapter(
                this,
                tmp,
                R.layout.activity_item,
                new String[]{"rank","username","score","time"},
                new int[]{R.id.rank,R.id.username,R.id.score,R.id.time});

        //添加并且显示
        list.setAdapter(listItemAdapter);

        //创建返回按钮
        Button backBtn = (Button) findViewById(R.id.backBtn);
        backBtn.setOnClickListener(this);

        //创建对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        setBuilder(builder, list);
        AlertDialog dialog = builder.create();

        //添加单击监听
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Map<String, Object> clap = (Map<String, Object>) arg0.getItemAtPosition(arg2);
                rank_clicked = (int) clap.get("rank");
                dialog.show();
            }
        });
    }

    private List<Map<String, Object>> getData() {
        ArrayList<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rank", "排名");
        map.put("username", "用户");
        map.put("score", "分数");
        map.put("time", "时间");
        listitem.add(map);

        List<ScoreRecord> records=scoreBoard.getAllRecord();
        int rk=1;
        SimpleDateFormat ft = new SimpleDateFormat ("MM-dd hh:mm");
        for (ScoreRecord record:records){
            map = new HashMap<String, Object>();
            map.put("rank",rk);
            map.put("score",record.getScore());
            map.put("username",record.getName());
            map.put("time",ft.format(record.getTime()));
            listitem.add(map);
            rk+=1;
        }
//        System.out.println(listitem.size());
        return listitem;
    }
    @Override
    public void onClick(View v){
        if (v.getId()==R.id.backBtn){
            Intent intent = new Intent(RankActivity.this, MainActivity.class);
            intent.putExtra("music",music);
            ActivityManager.getActivityManager().finishAllActivity();
            startActivity(intent);
        }
    }

    private void setBuilder(AlertDialog.Builder builder, ListView list){
        builder.setMessage("是否要删除该条记录");
        builder.setTitle("提示");
        builder.setPositiveButton("是", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                scoreBoard.doDelete(rank_clicked);
                List<Map<String, Object>> data_next = getData();
                SimpleAdapter listItemAdapter = new SimpleAdapter(
                        RankActivity.this,
                        data_next,
                        R.layout.activity_item,
                        new String[]{"rank","username","score","time"},
                        new int[]{R.id.rank,R.id.username,R.id.score,R.id.time});
                list.setAdapter(listItemAdapter);
            }
        });

        builder.setNegativeButton("否", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });
    }
}
